package com.yyfax.ph.base;

import com.yyfax.ph.BuildConfig;
import org.junit.runners.model.InitializationError;
import org.robolectric.RoboSettings;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.FileFsFile;
import org.robolectric.res.FsFile;
import org.robolectric.shadows.ShadowLog;
import rx.Scheduler;
import rx.android.plugins.RxAndroidPlugins;
import rx.android.plugins.RxAndroidSchedulersHook;
import rx.functions.Func1;
import rx.plugins.RxJavaHooks;
import rx.schedulers.Schedulers;

/**
 * Created by HenryZhang on 2017/6/5.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class BasicTestRunner extends RobolectricTestRunner {
	@Override
	protected Config buildGlobalConfig() {//自定义全局配置项
		return super.buildGlobalConfig();
	}

	public BasicTestRunner(Class<?> testClass) throws InitializationError {
		super(testClass);
		RoboSettings.setMavenRepositoryId("alimaven");
		RoboSettings.setMavenRepositoryUrl("http://maven.aliyun.com/nexus/content/groups/public/");
////		String buildVariant = BuildConfig.BUILD_TYPE + (BuildConfig.FLAVOR.isEmpty() ? "" : "/" + BuildConfig.FLAVOR);
//		String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR + "/") + BuildConfig.BUILD_TYPE;
//		System.setProperty("android.package", BuildConfig.APPLICATION_ID);
//		System.setProperty("android.manifest", "build/intermediates/manifests/full/" + buildVariant + "/AndroidManifest.xml");
////		System.setProperty("android.resources", "build/intermediates/res/" + buildVariant);
//		System.setProperty("android.resources", "build/intermediates/res/merged/" + buildVariant);
//		System.setProperty("android.assets", "build/intermediates/assets/" + buildVariant);


//		String buildVariant = BuildConfig.BUILD_TYPE + (BuildConfig.FLAVOR.isEmpty() ? "" : "/" + BuildConfig.FLAVOR);
		String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR + "/") + BuildConfig.BUILD_TYPE;
		System.setProperty("android.package", BuildConfig.APPLICATION_ID);
		System.setProperty("android.manifest", "build/intermediates/manifests/full/" + buildVariant + "/AndroidManifest.xml");
//		System.setProperty("android.resources", "build/intermediates/res/" + buildVariant);
		System.setProperty("android.resources", "build/intermediates/res/merged/" + buildVariant);
		System.setProperty("android.assets", "build/intermediates/assets/" + buildVariant);
	}

	@Override
	protected AndroidManifest getAppManifest(Config config) {
		AndroidManifest appManifest = super.getAppManifest(config);
		FsFile androidManifestFile = appManifest.getAndroidManifestFile();

		if (androidManifestFile.exists()) {
			return appManifest;
		} else {
			String moduleRoot = getModuleRootPath(config);
			androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("bundles", "manifests/full"));
			FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath().replace("/res", "").replace("bundles", "res"));
			FsFile assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath().replace("/assets", "").replace("bundles", "assets"));
			return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory);
		}
	}

	private String getModuleRootPath(Config config) {
		String moduleRoot = config.constants().getResource("").toString().replace("file:", "");
		return moduleRoot.substring(0, moduleRoot.indexOf("/build"));
	}

	public static void printLog() {
		ShadowLog.stream = System.out;
	}


	public static void asyncToSync() {
//		RxJavaPlugins.getInstance().registerSchedulersHook(new RxJavaSchedulersHook() {
//			@Override
//			public rx.Scheduler getIOScheduler() {
//				return Schedulers.immediate();
//			}
//		});
//		RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
//			@Override
//			public rx.Scheduler getMainThreadScheduler() {
//				return Schedulers.immediate();
//			}
//		});

		Func1<Scheduler, Scheduler> schedulerFunc = new Func1<Scheduler, Scheduler>() {
			@Override
			public Scheduler call(Scheduler scheduler) {
				return Schedulers.immediate();
			}
		};

		RxAndroidSchedulersHook rxAndroidSchedulersHook = new RxAndroidSchedulersHook() {
			@Override
			public Scheduler getMainThreadScheduler() {
				return Schedulers.immediate();
			}
		};

		RxJavaHooks.reset();
		RxJavaHooks.setOnIOScheduler(schedulerFunc);
		RxJavaHooks.setOnComputationScheduler(schedulerFunc);//对于有delay的Obserable做转换

		RxAndroidPlugins.getInstance().reset();
		RxAndroidPlugins.getInstance().registerSchedulersHook(rxAndroidSchedulersHook);
	}
}
